package com.gxa.saas.service.impl;

import com.gxa.saas.mapper.RouteDetailMapper;
import com.gxa.saas.pojo.RouteDetail;
import com.gxa.saas.service.RouteDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;


@Service
@Transactional(rollbackFor = Throwable.class)
public class RouteDetailServiceImpl implements RouteDetailService {

    @Autowired
    RouteDetailMapper routeDetailMapper;

    @Override
    public void up(Integer id) {
        List<RouteDetail> routeDetails = routeDetailMapper.selectAll();
        Integer num = routeDetails.get(0).getId();
        if(id.equals(num)){
            return;
        }

        routeDetailMapper.updateId(1000,id);
        Integer id1=id-1;
        RouteDetail routeDetail = routeDetailMapper.selectById(id1);
        while(routeDetail==null || routeDetail.equals("")){
            id1=id1-1;
            routeDetail= routeDetailMapper.selectById(id1);
        }
        routeDetailMapper.updateId(id,id1);
        routeDetailMapper.updateId(id1,1000);

    }

    @Override
    public void down(Integer id) {
        List<RouteDetail> routeDetails = routeDetailMapper.selectAll();
        RouteDetail routeDetail1 = routeDetails.get(routeDetails.size()-1);
        Integer num = routeDetail1.getId();

        if(id.equals(num)){
            return;
        }

        routeDetailMapper.updateId(2000,id);
        Integer id1=id+1;
        RouteDetail routeDetail = routeDetailMapper.selectById(id1);

        while(routeDetail==null || routeDetail.equals("")){
            id1=id1+1;
            routeDetail= routeDetailMapper.selectById(id1);
        }
        routeDetailMapper.updateId(id,id1);
        routeDetailMapper.updateId(id1,2000);
    }

    @Override
    public void first(Integer id) {
        List<RouteDetail> routeDetails = routeDetailMapper.selectAll();
        Integer id2= routeDetails.get(0).getId();
        Integer id1=id2-1;

        if(id.equals(id2)){
           return;
        }

        routeDetailMapper.updateId(id1,id);
    }

    @Override
    public void last(Integer id) {

        List<RouteDetail> routeDetails = routeDetailMapper.selectAll();
        Integer id2 = routeDetails.get(routeDetails.size() - 1).getId();
        Integer id1=id2+1;

        if(id.equals(id2)){
            return;
        }
        routeDetailMapper.updateId(id1,id);
    }

    @Override
    public void delete(Integer id) {
        RouteDetail routeDetail = routeDetailMapper.selectById(id);
        if(routeDetail==null || routeDetail.equals("")){
            return;
        }
        routeDetailMapper.delete(id);
    }

    @Override
    public void add(RouteDetail routeDetail) {
        routeDetailMapper.add(routeDetail);
    }


}
